package com.bw.shop2208a.cate

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.shop2208a.R
import com.bw.shop2208a.adapter.*
import com.bw.shop2208a.databinding.FragmentCateBinding
import com.bw.shop2208a.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


class CateFragment : BaseFragment<FragmentCateBinding, CateViewModel>() {
    val goodsTypeOneAdapter = GoodsTypeOneAdapter()
    val goodsTypeAdapter = GoodsTypeTwoAdapter()
    val goodsAdapter = GoodsAdapter2()
    var type = 0
    var page = 1
    override fun initViwe() {
        val dividerItemDecoration = DividerItemDecoration(context, View.SCROLL_AXIS_HORIZONTAL)
        binding.rv1.addItemDecoration(dividerItemDecoration)
        binding.rv3.addItemDecoration(dividerItemDecoration)
        binding.rv1.layoutManager = LinearLayoutManager(context)
        binding.rv1.adapter = goodsTypeOneAdapter
        binding.rv2.layoutManager = GridLayoutManager(context, 5)
        binding.rv2.adapter = goodsTypeAdapter
        binding.rv3.layoutManager = LinearLayoutManager(context)
        binding.rv3.adapter = goodsAdapter

        viewModel.getGoodsType()
        viewModel.getGoods(type,page,10)
        goodsTypeOneAdapter.setOnItemClickListener { adapter, view, position ->
            goodsTypeAdapter.setselectedPosition(0)
            val dataGoodsTypeEntity = goodsTypeOneAdapter.data[position]
            goodsTypeOneAdapter.setselectedPosition(position)
            goodsTypeAdapter.data.clear()
            goodsTypeAdapter.data.addAll(dataGoodsTypeEntity.data)
            goodsTypeAdapter.notifyDataSetChanged()
            page=1
            goodsAdapter.data.clear()
            if (dataGoodsTypeEntity.data.isNotEmpty()){
                type = dataGoodsTypeEntity.data[0].id
                viewModel.getGoods(type,page,10)
            }
        }
        goodsTypeAdapter.setOnItemClickListener { adapter, view, position ->
            goodsTypeAdapter.setselectedPosition(position)
            val dataXGoodsTypeEntity = goodsTypeAdapter.data[position]
            type = dataXGoodsTypeEntity.id
            goodsAdapter.data.clear()
            page=1
            viewModel.getGoods(type,page,10)
        }
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            val get = goodsAdapter.data[position]
            ARouter.getInstance().build("/app/goods").withSerializable("goods",get).navigation()
        }
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                goodsAdapter.data.clear()
                viewModel.getGoods(type,page,10)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.getGoods(type,++page,10)
                binding.srl.finishLoadMore()
            }
        })
    }

    override fun iniData() {
        viewModel.successGoodsType.observe(this) {
            if (it.code == 200) {
                goodsTypeOneAdapter.data.addAll(it.data)
                goodsTypeOneAdapter.notifyDataSetChanged()

                goodsTypeAdapter.data.addAll(it.data[0].data)
                goodsTypeAdapter.notifyDataSetChanged()
            }
        }
        viewModel.successGoods.observe(this){
            if (it.code==200){
                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_cate

    override fun getViewModel(): Class<CateViewModel> = CateViewModel::class.java

}